<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            /* 
            思路：为了满足更多小孩就不能让饼干浪费，所以大饼干优先提供给大胃口的孩子。当然
            也可以小饼干喂给小胃口的孩子
            */
            var findContentChildren = function (g, s) {
                g.sort((a, b) => a - b)
                s.sort((a, b) => a - b)
                //记录当前需要被满足的孩子
                let index = 0
                for (let i = 0; i < s.length; i++) {
                    //遍历饼干
                    if (index < g.length && g[index] <= s[i]) {
                        index++
                    }
                }
                return index
            }

            /* 思路②：先提供给大胃口的孩子 */
            var findContentChildren = function (g, s) {
                g.sort((a, b) => a - b)
                s.sort((a, b) => a - b)
                let index = s.length - 1
                //额外定义个count 用来记录被满足小孩的个数
                let count = 0
                for (let i = g.length - 1; i >= 0; i--) {
                    if (index >= 0 && g[i] <= s[index]) {
                        index--
                        count++
                    }
                }
                return count
            }
        </script>
    </body>
</html>
